﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;

namespace WPFSample.DiagonalizeTheButtonsSample
{
    public class DiagonalPanel : FrameworkElement
    {
        List<UIElement> Children = new List<UIElement>();

        Size sizeChildrenTotal;

        public Brush Background
        {
            get { return (Brush)GetValue(BackgroundProperty); }
            set { SetValue(BackgroundProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Background.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty BackgroundProperty =
            DependencyProperty.Register("Background", typeof(Brush), typeof(DiagonalPanel), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender));

        public void Add(UIElement el)
        {
            this.Children.Add(el);
            this.AddVisualChild(el);
            this.AddLogicalChild(el);
            this.InvalidateMeasure();
        }

        public void Remove(UIElement el)
        {
            this.Children.Remove(el);
            this.RemoveVisualChild(el);
            this.RemoveLogicalChild(el);
            this.InvalidateMeasure();
        }

        public int IndexOf(UIElement el)
        {
            return this.Children.IndexOf(el);
        }

        protected override int VisualChildrenCount
        {
            get
            {
                return this.Children.Count;
            }
        }

        protected override Visual GetVisualChild(int index)
        {
            if (index >= this.Children.Count)
            {
                throw new ArgumentOutOfRangeException("index");
            }

            return this.Children[index];
        }

        protected override Size MeasureOverride(Size availableSize)
        {
            this.sizeChildrenTotal = new Size(0, 0);
            foreach (UIElement child in this.Children)
            {
                child.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
                this.sizeChildrenTotal.Width += child.DesiredSize.Width;
                this.sizeChildrenTotal.Height += child.DesiredSize.Height;
            }

            return this.sizeChildrenTotal;
        }

        protected override Size ArrangeOverride(Size finalSize)
        {
            Point ptChild = new Point(0, 0);
            foreach (UIElement child in this.Children)
            {
                Size sizeChild = new Size(0, 0);
                sizeChild.Width = child.DesiredSize.Width * (finalSize.Width / this.sizeChildrenTotal.Width);
                sizeChild.Height = child.DesiredSize.Height * (finalSize.Height / this.sizeChildrenTotal.Height);
                child.Arrange(new Rect(ptChild, sizeChild));
                ptChild.X += sizeChild.Width;
                ptChild.Y += sizeChild.Height;
            }

            return finalSize;
        }

        protected override void OnRender(DrawingContext drawingContext)
        {
            drawingContext.DrawRectangle(this.Background, null, new Rect(0, 0, this.RenderSize.Width, this.RenderSize.Height));
        }
    }
}
